package com.doubao.strategy.tplStrategy;

import com.alibaba.fastjson.JSON;
import com.doubao.config.AppConfig;
import com.doubao.open.dto.message.WxMsg;
import com.doubao.pojo.ReqMsgDto;
import com.doubao.protocol.Msg;
import com.doubao.protocol.TextMsg;
import com.doubao.util.CallDubboUtil;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * description:ClientTplStrategy
 * <p>
 * author:sult
 * time: 2018/11/3 15:44
 **/
@Component
public class ClientTplStrategy implements TplStrategy {

    @Override
    public boolean sendTplMsg(Msg msg) throws Exception {
        TextMsg textMsg = (TextMsg)msg;
        String url = AppConfig.openUrl.replace("FROMID",textMsg.getFrom()).replace("TOID",textMsg.getTo()).replace("TOUSERTYPE",String.valueOf(textMsg.getToUserType()));

        String token=textMsg.getFrom();
        WxMsg wxMsg = new WxMsg();
        wxMsg.setToUser(token);

        //模板中的占位符
        Map<String, Object> tplMap = new HashMap<String, Object>();
        tplMap.put("toUser", token);
        tplMap.put("first", "您有新的未读消息");
        tplMap.put("reply", textMsg.getContent());
//        tplMap.put("remark", "点击查看详细");
        tplMap.put("url", url);



        ReqMsgDto dto = new ReqMsgDto();
        dto.setTemplateCode("EMAIL_TPL_MESSAGE_UUREAD_REMIND");
        dto.setTemplateParams(tplMap);
        dto.setMessage(wxMsg);

        String param = JSON.toJSONString(dto);
        CallDubboUtil.call("doubao.wechatMessage.WechatMessageService.sendMessage",param);

        return true;
    }

}
